package com.example.springpethospital.service;

import com.example.springpethospital.entity.*;
import com.example.springpethospital.mapper.*;
import com.example.springpethospital.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Integer LoginAndGetID(String username, String password) {
        return userMapper.GetIDwithUsernameAndPassword(username, password);
    }

    @Override
    public void ChangeUsername(String token, String newUsername) throws RuntimeException {
        if (newUsername == null || newUsername.isEmpty()) throw new IllegalArgumentException("用户名不得为空");
        if (userMapper.getIdwithUsername(newUsername) != null) throw new IllegalArgumentException("用户名已存在");
        IDAndName loginedUser = TokenUtil.verify(token);
        if (loginedUser == null) throw new RuntimeException("token验证失败，请重新登录");
        if (!userMapper.setUsername(loginedUser.id, newUsername))
            throw new RuntimeException("未找到相应用户");
    }

    @Override
    public void ChangePassword(String token, String oldPassword, String newPassword) throws RuntimeException {
        if (newPassword == null || newPassword.isEmpty()) throw new IllegalArgumentException("新密码不得为空");
        IDAndName loginedUser = TokenUtil.verify(token);
        if (loginedUser == null) throw new RuntimeException("token验证失败，请重新登录");
        if (!userMapper.setPassword(loginedUser.id, oldPassword, newPassword))
            throw new RuntimeException("未找到相应用户或旧密码错误");
    }

    @Override
    public int getUserType(int userid) {
        return userMapper.getUserType(userid);
    }

    @Override
    public boolean setUserType(int targetUserID, UserType targetType) {
        return userMapper.setUserType(targetUserID, targetType.toInteger());
    }

    @Override
    public void Register(String username, String password, UserType userType) throws IllegalArgumentException {
        if (username == null || username.isEmpty()) throw new IllegalArgumentException("用户名不可为空");
        if (password == null || password.isEmpty()) throw new IllegalArgumentException("密码不可为空");
        Integer id = userMapper.getIdwithUsername(username);
        if (id == null) {
            userMapper.registerUser(username, password, userType.toInteger());
        } else {
            throw new IllegalArgumentException("用户名已存在");
        }
    }

    @Override
    public boolean isAdmin(String token) {
        IDAndName user = TokenUtil.verify(token);
        if (user == null) return false;
        UserType usertype = UserType.toUserType(getUserType(user.id));
        return usertype == UserType.Admin;
    }

    @Override
    public List<UserDTO> searchUser(String username, Integer paging) {
        if (paging == null)
            return userMapper.searchUser(username);
        else {
            int offset = paging * Const.NUM_PER_PAGE;
            int limit = Const.NUM_PER_PAGE;
            return userMapper.searchUserWithPage(username, offset, limit);
        }
    }

    @Override
    public int getTotalPages(String username) {
        return (int) Math.ceil((double) userMapper.getUserNum(username) / (double) Const.NUM_PER_PAGE);
    }

    @Override
    public boolean removeUser(int userid) {
        return userMapper.removeUser(userid);
    }
}